package cc.vv.btpartyjob.activity

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import cc.dawning.utiming.lklocation.location.api.MyLocationListener
import cc.dawning.utiming.lklocation.location.utils.LocationUtils
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpRequest
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.util.download.request.AppVersionCheckingRequestObj
import cc.vv.baselibrary.util.download.response.AppVersionCheckingResponseObj
import cc.vv.baselibrary.util.toast
import cc.vv.baselibrary.view.imgselector.utils.LKPermissionUtil
import cc.vv.btpartyjob.bean.AppMainTabHostDataObj
import cc.vv.btpartyjob.binder.AppMainTableHostDataBinder
import cc.vv.btpartyjob.delegate.BaseTableHostDelegate
import cc.vv.btpartyjob.server.ApkDownloadServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.amap.api.location.AMapLocation
import com.dawning.utiming.login.api.LoginApi

/**
 *
 * Created by sun.li on 2018/9/5.
 * @author sun.li
 */
class AppMainActivity : BaseActivityMVP<BaseTableHostDelegate>() {

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)
        //设置全屏显示
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.statusBarColor = Color.TRANSPARENT
        }
    }


    override fun initData() {
        notifyModelChanged(AppMainTabHostDataObj())
        LKPermissionUtil.getInstance().requestAll(this)
        checkingApkVersion()

        LocationUtils.getInstance(this, object : MyLocationListener {
            override fun GetLocationSuccess(aMapLocation: AMapLocation?) {
                val province = aMapLocation?.province
                val city = aMapLocation?.city
                LKPrefUtil.putString("locationKey", province + city)
            }

            override fun GetLocationError() {

            }
        })
    }


    override fun initData(data: Bundle?) {
        super.initData(data)
    }


    override fun bindEvenListener() {

    }

    override fun getDelegateClass(): Class<BaseTableHostDelegate> {
        return BaseTableHostDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *>? {
        return AppMainTableHostDataBinder()
    }

    private var isFirstClickTime: Long = 0L

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - isFirstClickTime < 2000) {
                exitApp()
                finish()
                return true
            }
            "再按一次退出程序".toast()
            isFirstClickTime = System.currentTimeMillis()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    /**
     * 校验应用版本(版本更新)
     */
    private fun checkingApkVersion() {
        val obj = AppVersionCheckingRequestObj()
        BaseHttpRequest.getRequest(LoginApi.versionUpdata, obj,
                AppVersionCheckingResponseObj::class.java, false)
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is AppVersionCheckingResponseObj -> {
                val data = obj.data
//                data.url = "http://down.dibaqu.com/o_1cuj9of791d7n185c113i1if615om9.apk?sign=2dae3ef9057c646b98fb84e5d9e02a54&t=5c122509"
                ApkDownloadServer.versionHasBeenUpdated(this, data)
            }
        }
    }
}